package in.muton.edushare.pages.batch;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.curricula.CurriculaSummary;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.security.acl.Owner;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class BatchList extends EdusharePanel {
	private static final long serialVersionUID = -4844536567712547933L;

	public BatchList(String id, EduShareBasePage page, Departments dept,
			IModel<Curricula> c) {
		super(id, page);
		pageOwner = DEPT_PAGE;
		this.currModel = c;
		this.dept = new CayenneModel<Departments>(dept);
		init();
	}

	public BatchList(String id, EduShareBasePage page, Universities univ,
			IModel<Curricula> c) {
		super(id, page);
		pageOwner = UNIV_PAGE;
		this.currModel = c;
		this.univ = new CayenneModel<Universities>(univ);
		init();
	}

	private void init() {
		Form<Void> f = new Form<Void>("form");
		f.add(new Button("button") {
			@Override
			public void onSubmit() {
				PageParameters params;
				Curricula curr = currModel != null ? currModel.getObject()
						: null;
				if (pageOwner == DEPT_PAGE)
					params = NewBatch.getPageParameters(null, dept.getObject(),
							currModel != null ? currModel.getObject() : null,
							null);
				else
					params = NewBatch.getPageParameters(null, null, curr, univ.getObject());
				
				if(pageOwner == DEPT_PAGE)
					BatchList.this.replaceWith(new NewBatch(BatchList.this.getId(), params, basePage, BatchBase.fromDeptPage));
				else
					BatchList.this.replaceWith(new NewBatch(BatchList.this.getId(), params, basePage, BatchBase.fromUnivPage));
			}
			
			@Override
			public boolean isVisible(){
				if(pageOwner == DEPT_PAGE){
					DeptRoleCheck drc = new DeptRoleCheck(dept.getObject(), getLoggedInUser());
					if(drc.isPrimaryAdmin()||drc.isUnivAdmin())
						return true;
				}else{
					UnivRoleCheck urc = new UnivRoleCheck(univ.getObject(), getLoggedInUser());
					if(urc.isAdmin())
						return true;
				}
				return false;
			}
			private static final long serialVersionUID = -1853817260280015167L;
		});
		add(f);
		add(new ListView<IModel<Batches>>("programLV", new BatchModel()) {
			private static final long serialVersionUID = -2531139907771330450L;

			@Override
			protected void populateItem(final ListItem<IModel<Batches>> arg0) {
				Batches b = arg0.getModelObject().getObject();
				Link<Void> batchLink = 
					new Link<Void>("batchLink") {
						private static final long serialVersionUID = -1896073751972366323L;
	
						@Override
						public void onClick() {
							Batches b = arg0.getModelObject().getObject();
							PageParameters params = new PageParameters();
							params.put("0", b.getBatchId().toString());
							if(pageOwner == DEPT_PAGE)
								params.put("source", BatchBase.fromDeptPage);
							else
								params.put("source", BatchBase.fromUnivPage);
							
							setResponsePage(BatchSummary.class, params);
						}
					};
				String sem = b.getStartingSemester().getSemesterName();
				sem += ", " + b.getBatchYear();	
				batchLink.add(new Label("programName", b.getCurricula().getCurriculaName() + " ["+sem+"]"));
				arg0.add(batchLink);
				
//				arg0.add(new Label("semDetails", sem));
				arg0.add(new Link<Void>("program") {
					private static final long serialVersionUID = -1896073751972366323L;

					@Override
					public void onClick() {
						Batches b = arg0.getModelObject().getObject();
						PageParameters params = new PageParameters();
						params.put("0", b.getCurricula().getCurriculaId()
								.toString());
						setResponsePage(CurriculaSummary.class, params);
					}
				});

				arg0.add(new Link<Void>("delete") {
					private static final long serialVersionUID = -1896073751972366323L;

					@Override
					public void onClick() {
						Batches b = arg0.getModelObject().getObject();
						if (b.getUserBatchLink().size() > 0) {
							setError("Cannot remove the batch as there are students registered in it."
									+ "<br/>Please remove the students before proceeding.");
							return;
						}
						new BatchDAO(EduShareSession.get()
								.getSessionORMInterface()).removeBatch(b);
					}

					@Override
					public boolean isVisible() {
						if (pageOwner == DEPT_PAGE) {
							return DeptRoleCheck.isAdmin(dept.getObject(),
									getLoggedInUser());
						} else
							return UnivRoleCheck.isAdmin(univ.getObject(),
									getLoggedInUser());
					}
				});
			}
		});
	}

	private class BatchModel implements IModel<List<IModel<Batches>>> {
		private static final long serialVersionUID = -4448968824895606378L;

		public List<IModel<Batches>> getObject() {
			if (currModel != null)
				return new CayenneList<Batches>(currModel.getObject()
						.getBatches()).getObject();
			CayenneList<Batches> bl = new CayenneList<Batches>(null);
			List<Curricula> cl;
			if (pageOwner == DEPT_PAGE)
				cl = dept.getObject().getCurriculums();
			else
				cl = univ.getObject().getCurricula();
			for (Curricula c : cl) {
				bl.add(c.getBatches());
			}
			return bl.getObject();
		}

		public void setObject(List<IModel<Batches>> arg0) {
		}

		public void detach() {
		}
	}

	private final int DEPT_PAGE = 1;
	private final int UNIV_PAGE = 2;
	private int pageOwner;
	private IModel<Curricula> currModel = null;
	private IModel<Departments> dept = null;
	private IModel<Universities> univ = null;
}
